package com.zl.gmadmin.vo.menu;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.fasterxml.jackson.annotation.JsonInclude;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author zhangliang
 * @version 1.0
 * @date 2021/2/11 13:54
 */
@JsonInclude(JsonInclude.Include.NON_EMPTY)
public class MenuVo implements Serializable {

    private String name;

    private String path;

    private Boolean hidden;

    private String redirect;

    private String component;

    private Boolean alwaysShow;

    private MenuMetaVo meta;

    private List<MenuVo> children;

    public MenuVo(String name, String path, Boolean hidden, String redirect, String component, Boolean alwaysShow, MenuMetaVo meta, List<MenuVo> children) {
        this.name = name;
        this.path = path;
        this.hidden = hidden;
        this.redirect = redirect;
        this.component = component;
        this.alwaysShow = alwaysShow;
        this.meta = meta;
        this.children = children;
    }

    public MenuVo() {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public Boolean getHidden() {
        return hidden;
    }

    public void setHidden(Boolean hidden) {
        this.hidden = hidden;
    }

    public String getRedirect() {
        return redirect;
    }

    public void setRedirect(String redirect) {
        this.redirect = redirect;
    }

    public String getComponent() {
        return component;
    }

    public void setComponent(String component) {
        this.component = component;
    }

    public Boolean getAlwaysShow() {
        return alwaysShow;
    }

    public void setAlwaysShow(Boolean alwaysShow) {
        this.alwaysShow = alwaysShow;
    }

    public MenuMetaVo getMeta() {
        return meta;
    }

    public void setMeta(MenuMetaVo meta) {
        this.meta = meta;
    }

    public List<MenuVo> getChildren() {
        return children;
    }

    public void setChildren(List<MenuVo> children) {
        this.children = children;
    }

    public static MenuVo buildMenu(com.zl.gmadmin.service.dto.MenuDto menuDto) {
        MenuVo menuVo = new MenuVo();
        if (Objects.nonNull(menuDto)) {//如果菜单存在
            List<com.zl.gmadmin.service.dto.MenuDto> menuDtoList = menuDto.getChildren();  //获取下级菜单

            //如果组件不为空就返回
            menuVo.setName(ObjectUtil.isNotEmpty(menuDto.getComponentName()) ? menuDto.getComponentName() : menuDto.getTitle());
            // 一级目录需要加斜杠，不然会报警告
            menuVo.setPath(menuDto.getPid() == null ? "/" + menuDto.getPath() : menuDto.getPath());
            menuVo.setHidden(menuDto.getHidden());
            //如果不是外链接
            if (!menuDto.getiFrame()) {
                if (menuDto.getPid() == null) {
                    //一级菜单类型为Layout
                    menuVo.setComponent(StrUtil.isEmpty(menuDto.getComponent()) ? "Layout" : menuDto.getComponent());
                    // 如果不是一级菜单，并且菜单类型为目录，则代表是多级菜单
                } else if (menuDto.getType() == 0) {
                    menuVo.setComponent(StrUtil.isEmpty(menuDto.getComponent()) ? "ParentView" : menuDto.getComponent());
                } else if (StrUtil.isNotBlank(menuDto.getComponent())) {
                    menuVo.setComponent(menuDto.getComponent());
                }
            }
            menuVo.setMeta(new MenuMetaVo(menuDto.getTitle(),
                    menuDto.getIcon(), !menuDto.getCache()));
            if (CollectionUtil.isNotEmpty(menuDtoList)){
                menuVo.setAlwaysShow(true);
                menuVo.setRedirect("noredirect");
                menuVo.setChildren(menuDtoList.stream().map(MenuVo::buildMenu).collect(Collectors.toList()));
              //处理是一级菜单并且没有子菜单的情况
            }else  if (menuDto.getPid()==null){
                MenuVo menuVo1 = new MenuVo();
                menuVo1.setMeta(menuVo.getMeta());
                // 非外链
                if(!menuDto.getiFrame()){
                    menuVo1.setPath("index");
                    menuVo1.setName(menuVo.getName());
                    menuVo1.setComponent(menuVo.getComponent());
                } else {
                    menuVo1.setPath(menuDto.getPath());
                }
                menuVo.setName(null);
                menuVo.setMeta(null);
                menuVo.setComponent("Layout");
                List<MenuVo> list1 = new ArrayList<>();
                list1.add(menuVo1);
                menuVo.setChildren(list1);
            }
        }
        return menuVo;
    }
}
